package com.sgb.kjwl.viewmodel.home;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.core.util.Pair;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import com.sgb.goods.view.activity.inquiry.InquiryPublishingActivity;
import com.im.immodel.IMUtils;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.api.service.MailListAPI;
import com.sgb.kjwl.model.entity.UserAuthMsgEntity;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.model.entity.home.RefreshHomeNoticeEVent;
import com.sgb.kjwl.view.event.ChooseAreaEvent;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.link.utils.Preferences;
import com.sgb.member.entity.PermissionHuiYuanBean;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.event.EventConstant;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.view.ui.activity.manage.AgentTotalOrderActivity;
import com.sgb.kjwl.view.widget.self.permission.BasePermissionCallBack;
import com.sgb.kjwl.view.widget.self.permission.UserPermissionUtils;
import com.swgk.core.base.api.ICallBack;

import com.sgb.kjwl.model.entity.AdvertisementEntity;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.recyclerview.entity.PublishMenuEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.swgk.core.view.entity.PermissionEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.utils.DownLoadAdvUtil;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.AddCarInfoActivity;
import com.sgb.kjwl.view.ui.adapter.home.BottomMeumAdapter;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.link.router.PathLogistics;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainModel extends BaseViewModel {
    private final HomeRepertory repertory;
    private final MainActivity activity;

    public MainModel(MainActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void publishMenu(int tabTypeId) {
        if (tabTypeId == 4) {//租赁
            publishMenuByType("zulin");
        } else {
            publishMenu();
        }
    }

    //订单的权限
    private ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> mMenus = new ArrayList<>();
    private List<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> mTempMenus = new ArrayList<>();

    /**
     * 获取广告地址
     */
    public void getAdvUrl() {
        APIManager.getInstance().getCommonAPI().getTwinkleAdvertisement().enqueue(new ICallBack<BaseEntity<AdvertisementEntity>>() {
            @Override
            public void onSuccess(BaseEntity<AdvertisementEntity> data) {
                if (!TextUtils.isEmpty(data.getData().getPic())) {
                    String advUrlTag = MkvUtils.get().getString(MkvUtils.CommomData, CacheMarker.AdvUrlTag, "");
                    if (!data.getData().getPic().equals(advUrlTag)) {
                        try {
                            DownLoadAdvUtil.getFileFromServer(activity, data.getData().getPic());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AdvertisementEntity>> call, Throwable t) {

            }
        });
    }

    public void publishMenu() {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<MenuEntity>>() {
        }.getType();
        Object object = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CacheMenuMain, type);
        if (object != null) {
            DialogHelper.dismissProgressDialog();
            List<MenuEntity> cacheMateialDataList = (List<MenuEntity>) object;
            activity.showPublishPop(cacheMateialDataList);
        }
        APIManager.getInstance().getGoodsAPI().publishMenu().enqueue(new ICallBack<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity>> data) {
                if (data == null || !data.getState().equals(APIManager.RESPONSE_OK)) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.CacheMenuMain);
                    //获取从网络请求下来的数据的哈希值
                    String mHashNet = DatalUtils.getInstance().strToHashKey(data.getData());
                    if (!cacheMateialistlHash.equals(mHashNet)) {
                        activity.showPublishPop(data.getData());
                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.CacheMenuMain, data.getData());
                    }
                }
            }
        });
    }

    public void publishMenuByType(String typeName) {
        //+DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<MenuEntity>>() {
        }.getType();
        Object object = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CachePublishMenuByType + typeName, type);
        if (object != null) {
            DialogHelper.dismissProgressDialog();
            List<MenuEntity> cacheMateialDataList = (List<MenuEntity>) object;
            activity.showPublishPop(cacheMateialDataList);
        }
        APIManager.getInstance().getGoodsAPI().publishMenuByModuleType(typeName).enqueue(new ICallBack<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity>> data) {
                if (data == null || !data.getState().equals(APIManager.RESPONSE_OK)) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.CachePublishMenuByType + typeName);
                    //获取从网络请求下来的数据的哈希值
                    String mHashNet = DatalUtils.getInstance().strToHashKey(data.getData());
                    if (!cacheMateialistlHash.equals(mHashNet)) {
                        activity.showPublishPop(data.getData());
                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.CachePublishMenuByType + typeName, data.getData());
                    }
                }
            }
        });
    }

    public void checkToken(int type) {
        //DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().checkToken().enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (type == MainActivity.PUBLISH) {
                        activity.checkPublishData();
                    } else if (type == MainActivity.FRAGMENT_SHOPPING) {
                        activity.showShoppingCarView();
                    }
                } else {
                    if (!"Miss_Permission".equals(response.body().getCode())) {
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
                        repertory.saveToken(null);
                        activity.jumpLogin();
                    } else {
                        //弹框跳转认证
                        if (type == MainActivity.PUBLISH) {
                            activity.checkPublishData();
                        } else {
                            activity.showDialog();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

//    public void oneKeyLogin(OneKeyLoginEntity oneKeyLoginEntity) {
//        DialogHelper.showProgressDialog(activity, null, "登录中", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getUserAPI().oneClickLogin(oneKeyLoginEntity).enqueue(new Callback<BaseEntity<LoginEntity>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<LoginEntity>> call, Response<BaseEntity<LoginEntity>> response) {
//
//                DialogHelper.dismissProgressDialog();
//                if (response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    OneKeyLoginManager.getInstance().finishAuthActivity();
//                    if (response.body().getData().isNeedRegister()) {
//                        Intent intent = new Intent(activity, SetPwdActivity.class);
//                        intent.putExtra("mobile", response.body().getData().getPhone());
//                        activity.startActivity(intent);
//                    } else {
//                        //登录成功逻辑
//                        SharedPreferenceUtil.getInstance().cleatData();
//                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, response.body().getData().getToken());
//                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, response.body().getData().getPhone());
//                        JPushInterface.setAlias(activity, 1, response.body().getData().getPhone());
//
//                        loginImModel(response);
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<LoginEntity>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//    }

//    //网易IM登陆
//    private void loginImModel(Response<BaseEntity<LoginEntity>> response) {
//        NimUIKit.login(new LoginInfo(response.body().getData().getImAccount(), response.body().getData().getImToken()), new RequestCallback<LoginInfo>() {
//            @Override
//            public void onSuccess(LoginInfo param) {
//                DialogHelper.dismissProgressDialog();
//                saveUserInfo(response.body().getData());
//                activity.loginSucceed(response.body().getData(), response.body().getData().getPhone());
//                getUserInfo();
//            }
//
//            @Override
//            public void onFailed(int code) {
//                DialogHelper.dismissProgressDialog();
//            }
//
//            @Override
//            public void onException(Throwable exception) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//    }
        //1.5.7 商品首页询价新增token失效的校验，后端提议用这个接口来判断
        public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    Intent createGoods = new Intent(activity, InquiryPublishingActivity.class);
                    activity.startActivity(createGoods);
                } else {
                    MToast.showToast(activity,"登录过期,请重新登录");
                    ActivityJump.startActivity(AppRouteConstant.VerificationCodeLoginActivity, Pair.create("isJump", false));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getLogisticsPermissions(boolean getMenu) {
        //获取用户数据
        //TODO 接口有时候报错，改为不提示报错内容
        NewAPIManager.getInstance().getLogisticsAPI().getLogisticsPermissions().enqueue(new Callback<BaseEntity<PermissionEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PermissionEntity>> call, Response<BaseEntity<PermissionEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    UserOperating.getInstance().setUserInfo(response.body().getData());
                    if (getMenu) {
                        if (UserOperating.getInstance().isPersonal()) {
                            SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, "0");
                        } else if (UserOperating.getInstance().isTeam() && UserOperating.getInstance().getUserInfo() != null && UserOperating.getInstance().getUserInfo().getTeamRespVO() != null) {
                            SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, UserOperating.getInstance().getUserInfo().getTeamRespVO().getNo());
                        } else {
                            SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, response.body().getData().getCompanyNo());
                        }
                        getPermissionMenu(activity);
                    } else {
                        activity.setLogisticsWork();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PermissionEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void saveUserInfo(LoginEntity loginEntity) {
        repertory.saveToken(loginEntity.getToken());
        repertory.saveAccount(loginEntity.getPhone());

//        NimUIKit.setAccount(loginEntity.getImAccount());
//        AVChatKit.setAccount(loginEntity.getImAccount());
        //Preferences.saveUserAccount(loginEntity.getImAccount());
 //       Preferences.saveUserToken(loginEntity.getImToken());
    }

    /**
     * 通过标识来获取跳转连接
     *
     * @param map
     */
    public void getStringUrl(Map<String, String> map) {
        String name = map.get("name");
        String type = map.get("type");
        String typeUrl = map.get("typeUrl");
        String param = type;
        final String urlId = typeUrl;
        APIManager.getInstance().getPickAPI().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + urlId;
                    WebViewX5HostActivity.start(activity.getActivity(), url);
                }
            }
        });
    }

    /**
     * 获取我的页面菜单数据
     */
    public void getPermissionMenu(MainActivity activity) {
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        String comNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "0");
        //返回页面数据
        APIManager.getInstance().getUserAPI().getPermissionMenu(comNo, type).enqueue(new Callback<BaseEntity<List<PermissionMenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PermissionMenuEntity>>> call, Response<BaseEntity<List<PermissionMenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response != null && response.body() != null && response.body().getData() != null) {
                    Gson gson = new Gson();
                    String s = gson.toJson(response.body().getData(), new TypeToken<List<PermissionMenuEntity>>() {
                    }.getType());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.IS_DATA, s);
                    activity.setLogisticsWork();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionMenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*
     *更新会员权限
     */
    public void getPermissionHuiYuan() {
        //返回页面数据
        APIManager.getInstance().getUserAPI().getPermissionHuiYuan().enqueue(new Callback<BaseEntity<List<PermissionHuiYuanBean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PermissionHuiYuanBean>>> call, Response<BaseEntity<List<PermissionHuiYuanBean>>> response) {
                if (response != null && response.body() != null && response.body().getData() != null) {
                    String s = new Gson().toJson(response.body().getData(), new TypeToken<List<PermissionMenuEntity>>() {}.getType());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERMISSION_HUI_YUAN, s);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionHuiYuanBean>>> call, Throwable t) {
            }
        });
    }

    //动态添加底部菜单
    public void categoryDisplayData(int index, boolean ispersonal, BottomMeumAdapter bottomMeumAdapter) {
        ArrayList<MenuEntity> menulist = new ArrayList<>();
        switch (index) {
            case 1:
                if (!ispersonal) {
                    MenuEntity<Object> objectMenuEntity1 = new MenuEntity<>();
                    objectMenuEntity1.setImageResource(R.drawable.ic_home_daiban);
                    objectMenuEntity1.setName("待办");
                    menulist.add(objectMenuEntity1);
                }
                MenuEntity<Object> objectMenuEntity2 = new MenuEntity<>();
                objectMenuEntity2.setImageResource(R.drawable.ic_home_tongxunlu);
                objectMenuEntity2.setName("通讯录");
                menulist.add(objectMenuEntity2);
                MenuEntity<Object> objectMenuEntity3 = new MenuEntity<>();
                objectMenuEntity3.setImageResource(R.drawable.ic_home_gouwuche);
                objectMenuEntity3.setName("采购单");
                menulist.add(objectMenuEntity3);
//                MenuEntity<Object> objectMenuEntity4 = new MenuEntity<>();
//                objectMenuEntity4.setImageResource(R.drawable.ic_home_shangji);
//                objectMenuEntity4.setName("商机");
//                menulist.add(objectMenuEntity4);
                MenuEntity<Object> objectMenuEntity5 = new MenuEntity<>();
                objectMenuEntity5.setImageResource(R.drawable.ic_home_invite);
                objectMenuEntity5.setName("邀请");
                menulist.add(objectMenuEntity5);
                MenuEntity<Object> objectMenuEntity6 = new MenuEntity<>();
                objectMenuEntity6.setImageResource(R.drawable.ic_home_apply);
                objectMenuEntity6.setName("申请");
                menulist.add(objectMenuEntity6);
                MenuEntity<Object> objectMenuEntity7 = new MenuEntity<>();
                objectMenuEntity7.setImageResource(R.drawable.ic_home_client);
                objectMenuEntity7.setName("会员");
                menulist.add(objectMenuEntity7);
                MenuEntity<Object> objectMenuEntity8 = new MenuEntity<>();
                objectMenuEntity8.setImageResource(R.drawable.ic_home_quotation);
                objectMenuEntity8.setName("报价");
                menulist.add(objectMenuEntity8);
                MenuEntity<Object> objectMenuEntity1_9 = new MenuEntity<>();
                objectMenuEntity1_9.setImageResource(R.drawable.icon_order);
                objectMenuEntity1_9.setName("订单");
                menulist.add(objectMenuEntity1_9);
                MenuEntity<Object> objectMenuEntity1_10 = new MenuEntity<>();
                objectMenuEntity1_10.setImageResource(R.drawable.icon_home_config);
                objectMenuEntity1_10.setName("设置首页");
                menulist.add(objectMenuEntity1_10);
                MenuEntity<Object> objectMenuEntity1_11 = new MenuEntity<>();
                objectMenuEntity1_11.setImageResource(R.drawable.icon_qbg);
                objectMenuEntity1_11.setName("企管帮");
                menulist.add(objectMenuEntity1_11);
                break;
            case 2:
                if (!ispersonal) {
                    MenuEntity<Object> objectMenuEntity9 = new MenuEntity<>();
                    objectMenuEntity9.setImageResource(R.drawable.ic_home_daiban);
                    objectMenuEntity9.setName("待办");
                    menulist.add(objectMenuEntity9);
                }
                MenuEntity<Object> objectMenuEntity10 = new MenuEntity<>();
                objectMenuEntity10.setImageResource(R.drawable.ic_home_tongxunlu);
                objectMenuEntity10.setName("通讯录");
                menulist.add(objectMenuEntity10);
//                MenuEntity<Object> objectMenuEntity11 = new MenuEntity<>();
//                objectMenuEntity11.setImageResource(R.drawable.ic_home_shangji);
//                objectMenuEntity11.setName("商机");
//                menulist.add(objectMenuEntity11);
                MenuEntity<Object> objectMenuEntity12 = new MenuEntity<>();
                objectMenuEntity12.setImageResource(R.drawable.ic_home_invite);
                objectMenuEntity12.setName("邀请");
                menulist.add(objectMenuEntity12);
                MenuEntity<Object> objectMenuEntity13 = new MenuEntity<>();
                objectMenuEntity13.setImageResource(R.drawable.ic_home_apply);
                objectMenuEntity13.setName("申请");
                menulist.add(objectMenuEntity13);
                MenuEntity<Object> objectMenuEntity14 = new MenuEntity<>();
                objectMenuEntity14.setImageResource(R.drawable.ic_home_client);
                objectMenuEntity14.setName("会员");
                menulist.add(objectMenuEntity14);
                MenuEntity<Object> objectMenuEntity15 = new MenuEntity<>();
                objectMenuEntity15.setImageResource(R.drawable.ic_home_quotation);
                objectMenuEntity15.setName("报价");
                menulist.add(objectMenuEntity15);
                MenuEntity<Object> objectMenuEntity2_16 = new MenuEntity<>();
                objectMenuEntity2_16.setImageResource(R.drawable.icon_order);
                objectMenuEntity2_16.setName("订单");
                menulist.add(objectMenuEntity2_16);
                MenuEntity<Object> objectMenuEntity2_17 = new MenuEntity<>();
                objectMenuEntity2_17.setImageResource(R.drawable.icon_home_config);
                objectMenuEntity2_17.setName("设置首页");
                menulist.add(objectMenuEntity2_17);
                MenuEntity<Object> objectMenuEntity2_18 = new MenuEntity<>();
                objectMenuEntity2_18.setImageResource(R.drawable.icon_qbg);
                objectMenuEntity2_18.setName("企管帮");
                menulist.add(objectMenuEntity2_18);
                break;
        }
        bottomMeumAdapter.setDatas(menulist);
    }

    /**
     * 判断是否需要完善车辆信息
     * ps：只有一辆车的时候才检测
     *
     * @param
     */
    public void checkCarInfo() {
        DialogHelper.showProgressDialog(activity, "请求中...");
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new ICallBack<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CarInfo>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getData() == null) {
                    return;
                }
                List<CarInfo> carInfos = data.getData();
                if (carInfos.size() > 0) {
                    // 只判断有且只有一辆车的时候车辆信息是否完善
                    boolean needComplete;
                    if (carInfos.size() == 1 && carInfos.get(0) != null) {
                        if (carInfos.get(0).getIsComplete() == 1) {
                            needComplete = false;
                        } else {
                            needComplete = true;
                        }
                    } else {
                        needComplete = false;
                    }
                    if (!needComplete) {
                        ARouter.getInstance().build(PathLogistics.CarriagePushActivity).navigation();
                    } else {
                        new RoundDialog.Builder(activity)
                                .title("提示")
                                .content("请先完善" + carInfos.get(0).getCarNumber() + "的车辆信息")
                                .cancel("暂不完善")
                                .confirm("确定")
                                .cancelable(false)
                                .confirmCallback(v -> {
                                    Intent intent = new Intent(activity, AddCarInfoActivity.class);
                                    intent.putExtra("carNo", Long.valueOf(carInfos.get(0).getNo()));
                                    activity.startActivity(intent);
                                })
                                .show();

                    }
                }
            }
        });
    }

    /**
     * 个人发布无服务
     *
     * @return
     */
    public List<MenuEntity> getEnterpriseListPerson() {
        List<MenuEntity> list = new ArrayList<>();
        list.add(new MenuEntity("1", "标", "商标转让"));
        list.add(new MenuEntity("2", "专", "专利转让"));
        list.add(new MenuEntity("3", "版", "版权转让"));
        list.add(new MenuEntity("4", "企", "公司转让"));

        list.add(new MenuEntity("5", "资", "资产转让"));
        list.add(new MenuEntity("6", "标", "找商标"));
        list.add(new MenuEntity("7", "专", "找专利"));

        list.add(new MenuEntity("8", "版", "找版权"));
        list.add(new MenuEntity("9", "企", "找公司"));
        list.add(new MenuEntity("10", "资", "找资产"));
        list.add(new MenuEntity("11", "服", "找服务"));
        return list;
    }

    /**
     * 企业、团队发布有服务
     *
     * @return
     */
    public List<MenuEntity> getEnterpriseList() {
        List<MenuEntity> list = new ArrayList<>();
        list.add(new MenuEntity("1", "转", "转让发布"));
        list.add(new MenuEntity("2", "服", "服务发布"));
        list.add(new MenuEntity("3", "需", "需求发布"));
        list.add(new MenuEntity("4", "报", "需求报价"));
        list.add(new MenuEntity("5", "认", "团队认证"));
        list.add(new MenuEntity("6", "邀", "邀请报价"));
        list.add(new MenuEntity("7", "加", "邀请加盟"));
        list.add(new MenuEntity("8", "申", "申请加盟"));
        list.add(new MenuEntity("9", "销", "销售订单"));
        list.add(new MenuEntity("10", "采", "采购订单"));
        return list;
    }

    /**
     * 会员底部弹出框数据来源
     *
     * @return
     */
    public List<PublishMenuEntity> getMemberLIst(boolean isPerson) {
        List<PublishMenuEntity> list = new ArrayList<>();
        list.add(new PublishMenuEntity("1", "盟", "申请加盟"));
        list.add(new PublishMenuEntity("2", "邀", "邀请会员"));
        list.add(new PublishMenuEntity("3", "审", "会员审核"));
        if (!isPerson) {
            list.add(new PublishMenuEntity("4", "企", "关注我的"));
            list.add(new PublishMenuEntity("5", "关注", "我的关注"));
        }
        return list;
    }

    /**
     * 物流底部弹框数据来源
     *
     * @return
     */
    public List<PublishMenuEntity> getLogisticsList() {
        List<PublishMenuEntity> list = new ArrayList<>();
        list.add(new PublishMenuEntity("1", "货", "发布货运"));
       // list.add(new PublishMenuEntity("2", "竞", "发布竞价"));
        list.add(new PublishMenuEntity("3", "车", "发布车辆"));
        list.add(new PublishMenuEntity("4", "承", "发布拉货"));
        return list;

    }

    /**
     * 工队发布数据
     *
     * @return
     */
    public List<PublishMenuEntity> getPersonTeamList(int type) {
        List<PublishMenuEntity> list = new ArrayList<>();
        switch (type){
            case 0:
                list.add(new PublishMenuEntity("1", "工", "工人发布"));
                list.add(new PublishMenuEntity("4", "队", "工队发布"));
                break;
            case 2:
                list.add(new PublishMenuEntity("4", "队", "工队发布"));
                list.add(new PublishMenuEntity("2", "用", "用工发布"));
                list.add(new PublishMenuEntity("3", "劳", "劳务发布"));
                break;
            default:
                break;
        }
        return list;
    }

    /**
     * 工队发布数据
     *
     * @return
     */
    public List<PublishMenuEntity> getComList() {
        List<PublishMenuEntity> list = new ArrayList<>();
        list.add(new PublishMenuEntity("5", "队", "工队发布"));
        list.add(new PublishMenuEntity("2", "用", "用工发布"));
        list.add(new PublishMenuEntity("3", "劳", "劳务发布"));
        return list;
    }

    /**
     * 租赁发布数据
     *
     * @return
     */
    public List<PublishMenuEntity> getLeaseList() {
        List<PublishMenuEntity> list = new ArrayList<>();
        list.add(new PublishMenuEntity("", "械", "求租机械"));
        list.add(new PublishMenuEntity("", "设", "求租设备"));
        list.add(new PublishMenuEntity("", "车", "求租车辆"));
        list.add(new PublishMenuEntity("", "房", "求租房产"));
        list.add(new PublishMenuEntity("", "地", "求租土地"));
        list.add(new PublishMenuEntity("", "厂", "求租工厂"));
        list.add(new PublishMenuEntity("", "农", "求租农机"));
        return list;
    }

    /**
     * 判断用户是否有经营商品-我的报价权限
     */
    public void isHasQuotationAuth() {
        if (activity == null)
            return;
        DialogHelper.showProgressDialog(activity, "请求中...");
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        String comNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "0");
        APIManager.getInstance().getUserAPI().getPermissionMenu(comNo, type).enqueue(new Callback<BaseEntity<List<PermissionMenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PermissionMenuEntity>>> call, Response<BaseEntity<List<PermissionMenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response != null && response.body() != null && response.body().getData() != null) {
                    Gson gson1 = new Gson();
                    String s1 = gson1.toJson(response.body().getData(), new TypeToken<List<PermissionMenuEntity>>() {
                    }.getType());
                    if (s1.contains("商品经营")) {
                        for (PermissionMenuEntity entity : response.body().getData()) {
                            if (TextUtils.equals(entity.getTitle(), "商品经营")) {
                                Gson gson = new Gson();
                                String s = gson.toJson(entity.getList(), new TypeToken<List<PermissionMenuEntity.ListEntity>>() {
                                }.getType());
                                if (s.contains("我的报价")) {
                                    activity.resultHasQuotationAuth(true);
                                } else {
                                    activity.resultHasQuotationAuth(false);
                                }
                                break;
                            }
                        }
                    } else {
                        activity.resultHasQuotationAuth(false);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionMenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取我的-待办订单权限
     */
    public void isHasOrderAuth() {
        if (activity == null)
            return;
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        String comNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        List<PermissionMenuEntity> data = DatalUtils.getInstance().getData(MkvUtils.CacheUserCenterMenu, CacheMarker.USER_CENTER_MENU + comNo, new TypeToken<List<PermissionMenuEntity>>() {
        });
        if (data != null) {
            getPermissionMenus(data);
            return;
        }
        DialogHelper.showProgressDialog(activity, "请求中...");
        APIManager.getInstance().getUserAPI().getPermissionMenu(comNo, type).enqueue(new Callback<BaseEntity<List<PermissionMenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PermissionMenuEntity>>> call, Response<BaseEntity<List<PermissionMenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response != null && response.body() != null && response.body().getData() != null) {
                    getPermissionMenus(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionMenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*获取权限判断跳转我的待办订单界面*/
    private void getPermissionMenus(List<PermissionMenuEntity> data) {
        UserPermissionUtils.getInstance().getPermission(data, new BasePermissionCallBack() {

            //企业 头部代办区域订单权限
            @Override
            public void getOrderPermission(List<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> orderMenus) {
                mTempMenus.clear();
                mMenus.clear();
                mTempMenus.addAll(orderMenus);
                mMenus.addAll(mTempMenus);
                AgentTotalOrderActivity.Companion.launch(activity, mMenus);
            }
        });
    }

    /**
     * 去团队列表
     */
    public void gotTeamAuthListActivity() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getAuthenticationUserInfo().enqueue(new Callback<BaseEntity<UserAuthMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserAuthMsgEntity>> call, Response<BaseEntity<UserAuthMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.gototeamList(response);
                } else {
                    Toast.makeText(activity, response.body().getMsg(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserAuthMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 底部集采菜单menu
     *
     * @return
     */
    public List<PublishMenuEntity> getJiCaiMenuList() {
        List<PublishMenuEntity> list = new ArrayList<>();
        list.add(new PublishMenuEntity("1", "需", "需方发起"));
        list.add(new PublishMenuEntity("3", "供", "供方发起"));
        list.add(new PublishMenuEntity("2", "企", "企业专项"));
        return list;
    }

    /**
     * 公司列表信息
     */
    public void getCompanyList() {
        APIManager.getInstance().getMailListAPI().getUserCompany(MailListAPI.UserCenterUrl.URL_GET_USER_COMPANY).enqueue(new Callback<BaseEntity<List<CompanyEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CompanyEntity>>> call, Response<BaseEntity<List<CompanyEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    showCompanyList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CompanyEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void showCompanyList(List<CompanyEntity> data) {
        ////判断企业列表个数大于1时则必然有企业或者组织/团队等需要做必要数据存储
        if (data.size() > 1) {
            String no = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
            if (!TextUtils.isEmpty(no)) {
                for (CompanyEntity company : data) {
                    //找到主企业
                    if (null != company && company.getCompNo().equals(no)) {
                        setCurrentCompany(company);
                    }
                    break;
                }
            } else {
                setCurrentCompany(data.get(0));
            }
        } else {
            activity.initMeMenu();
        }
    }

    // 设置主企业
    public void setCurrentCompany(CompanyEntity companyEntity) {
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMP_LOGO, companyEntity.getCompLogo());
        //SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, companyEntity.getCompNo());
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, companyEntity.getCompName());
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, companyEntity.getType());
        //发送我的界面
        //EventBus.getDefault().postSticky(MainEvent.make(EventConstant.EVENT_MAIN_ENTERPRISE_SWITCHING, 0));
        // 发送到Home界面
        EventBus.getDefault().postSticky(new RefreshHomeNoticeEVent(EventConstant.EVENT_NOTICE_REFRESH_HOME));
        activity.initMeMenu();
    }

}
